package com.df.bpm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.df.bpm.entity.*;
import com.df.bpm.service.impl.FailModeServiceImpl;
import com.df.bpm.service.impl.RequirementFMServiceImpl;
import com.df.common.entity.Pagination;
import com.df.common.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * 失效模式控制器
 */
@Slf4j
@Tag(name = "失效模式控制器")
@RequestMapping("/failMode")
@RequiredArgsConstructor
@RestController
@SaCheckLogin
public class FailModeController {
    private final FailModeServiceImpl failModeServiceImpl;
    private final RequirementFMServiceImpl requirementFMServiceImpl;

    /**
     * 查询失效模式
     * @param pagination 分页信息
     * @param temp 失效模式线索
     * @return  失效模式列表
     */
    @Operation(summary = "查询失效模式", description = "查询失效模式")
    @GetMapping("")
    @SaIgnore
    public ResponseEntity<?> getFailMode(Pagination pagination, FailMode temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(failModeServiceImpl.getList4Like(temp)));
        } else {
            return ResponseEntity.ok(failModeServiceImpl.getList4Like(temp));
        }
    }

    /**
     * 新增失效模式
     * @param temp   失效模式信息
     * @return  新增成功后的失效模式信息
     */
    @Operation(summary = "新增失效模式", description = "新增失效模式")
    @PostMapping("")
    @SaIgnore
    public ResponseEntity<?> newFailMode(@RequestBody FailMode temp) {
        failModeServiceImpl.add(temp);
        return ResponseEntity.ok(temp);
    }

    /**
     * 关联申请单
     * @param temp   关联关系
     * @return  关联关系
     */
    @Operation(summary = "关联申请单", description = "关联申请单")
    @PostMapping("bindRequirement")
    @SaIgnore
    public ResponseEntity<?> bindRequirement(@RequestBody RequirementFM temp) {
        requirementFMServiceImpl.add(temp);
        return ResponseEntity.ok(temp);
    }

}
